/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Bobok
 */
public class EmailValidator implements Validator{
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        /* Create the correct mask */
        Pattern mask = null;
        mask = Pattern.compile("[A-ZŻŁÓŃĆĘĄŹŚa-zżźćńółśąę0-9_\\-\\.]+@[A-ZŻŁÓŃĆĘĄŹŚa-zżźćńółśąę0-9_\\-]+\\.[A-ZŻŁÓŃĆĘĄŹŚa-zżźćńółśąę0-9_\\-]+(\\.[A-ZŻŁÓŃĆĘĄŹŚa-zżźćńółśąę0-9_\\-]+)*");

        /* Get the string value of the current field */
        String emailField = (String) value;

        /* Check to see if the value is a zip code */
        Matcher matcher = mask.matcher(emailField);

        if (!matcher.matches()) {

            FacesMessage message = new FacesMessage();
            message.rendered();
            message.setDetail("Not valid email (example: example@example.com)");
            message.setSummary("Not valid email (example: example@example.com)");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }

    }
}
